package com.yuyang.springcloud.controller;


import com.yuyang.springcloud.entities.CommonResult;
import com.yuyang.springcloud.entities.Payment;
import com.yuyang.springcloud.mylb.LoadBalanced;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yu yang
 * @date 2022/10/25 -16:14
 */
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    //    private final String urlPrefix = "http://localhost:8001";
    // 通过在eureka上注册过的微服务名称调用
    public static final String PAYMENT_SRV = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalanced loadBalanced;

    @PostMapping("/consumer/payment/create")
    public CommonResult create(Payment payment){
        return restTemplate.postForObject(PAYMENT_SRV+"/payment/create",payment,CommonResult.class);
    }
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_SRV+"/payment/get/"+id,CommonResult.class,id);
    }
    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult getPaymentById2(@PathVariable("id") Long id){
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_SRV + "/payment/get/" + id, CommonResult.class, id);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        }
        return CommonResult.error(444,"操作失败");
    }
    @PostMapping("/consumer/payment/createForEntity")
    public CommonResult create2(Payment payment){
        return restTemplate.postForEntity(PAYMENT_SRV+"/payment/create",payment,CommonResult.class).getBody();
    }

    @GetMapping("/payment/get/lb")
    public CommonResult myLB(){
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (serviceInstances == null | serviceInstances.size() <= 0){
            return CommonResult.error(444,"没有注册的服务");
        }
        ServiceInstance serverInstance = loadBalanced.getServerInstance(serviceInstances);
        return CommonResult.success(200,"success",serverInstance.getPort());
    }
    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://CLOUD-PAYMENT-SERVICE"+"/payment/zipkin/", String.class);
        return result;
    }


}
